package com.wholesale;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class SearchLayout extends LinearLayout implements OnClickListener {   
	private ViewFlipper viewFlipper ;
	private Context context ;
	private View baseView ;
	private ListView bodyList ;
	private Button leftButton ;
	private Button rightButton ;
	private TextView headView ;
	private EditText keyWord ;
	//  构造函数  
	public SearchLayout(Context context,final ViewFlipper viewFlipper){
		super(context);
		this.viewFlipper = viewFlipper ;
		this.context = context ;
		//加载需要的属性，加载方法一的子Layout  
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.search, this);  
		bodyList = (ListView)baseView.findViewById(R.id.bodyList);
		leftButton = (Button)baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button)baseView.findViewById(R.id.rightBtn);
		rightButton.setText("Search");
		rightButton.setOnClickListener(this);
		headView = (TextView)baseView.findViewById(R.id.title);
		headView.setText("Search");
		
		keyWord = (EditText)baseView.findViewById(R.id.keyword);
        // item 单击事件
        bodyList.setOnItemClickListener(itemClieckListener);
	}
	//ListView 点击事件
    OnItemClickListener itemClieckListener = new OnItemClickListener() {
    	public void onItemClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			
		}
    };
	public void onClick(View v) {
		InputMethodManager imm = (InputMethodManager)((Activity) getContext()).getSystemService(Context.INPUT_METHOD_SERVICE);  
		switch (v.getId()) {
		case R.id.leftBtn:
		
			imm.hideSoftInputFromWindow(leftButton.getWindowToken(), 0);
			
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
		
			imm.hideSoftInputFromWindow(rightButton.getWindowToken(), 0);
			String keyWords = keyWord.getText().toString().trim();
			if(keyWords.equals("")){
				
			}else{
				SearchResultLayout searchResultLayout = new SearchResultLayout(context, viewFlipper,keyWords);
				viewFlipper.addView(searchResultLayout);
				viewFlipper.showNext();
			}
			break;
		default:
			break;
		}
		
	}
}
